package com.siyao.redvip.ui.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.text.format.DateUtils;
import android.view.View;
import android.os.Bundle;

import com.android.volley.http.RequestManager;
import com.android.volley.http.RequestManager.RequestListener;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.siyao.redvip.R;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.content.Context;
import android.content.Intent;
import android.widget.TextView;
import android.widget.AdapterView;

import com.siyao.redvip.bean.FriendCircle;
import com.siyao.redvip.bean.HDxxBean;
import com.siyao.redvip.bean.TResponseBean;
import com.siyao.redvip.components.base.BaseFragmentActivity;
import com.siyao.redvip.components.emojicon.EmoteInputView;
import com.siyao.redvip.components.emojicon.EmoticonsEditText;
import com.siyao.redvip.network.NetConstans;
import com.siyao.redvip.ui.adapter.FriendAdapter;
import com.siyao.redvip.ui.adapter.FriendAdapter.ButtonCallBack;
import com.siyao.redvip.utils.ToastUtils;
import com.siyao.redvip.utils.Track;

import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView.OnItemClickListener;

/**
 * 朋友圈
 * 
 * @author lenovo
 * 
 */
public class FriendActivity extends BaseFragmentActivity implements
		 ButtonCallBack{

//	private Button btn_user_comment_emo;// 表情按钮
//
//	private EmoticonsEditText edit_user_comment;// 表情布局
//
//	private TextView txtbtn_user_comment_send;// 发送评论

	private EmoteInputView mInputView;// 表情布局

	private PullToRefreshListView lvReply;
	ListView actualListView;
	private int currPage;// 当前页
	private int total;// 总页数
	private List<FriendCircle>dataList;//朋友圈的信息
	private FriendAdapter friendAdapter;
	private int tmpPage;//存零时变量

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleText("朋友圈");
		setLeftButton("返回");
		setRightButton(R.drawable.chat_img_selector);
	}

	@Override
	protected void beforeInit() {

	}

	@Override
	public void ininlazeLayout() {
		View appView = getLayoutInflater().inflate(
				R.layout.activity_friend_fragment, null);
		appMainView.addView(appView, layoutParams);
	}

	@Override
	public void ininlazeView() {
		lvReply = (PullToRefreshListView) findViewById(R.id.lvReply);
		lvReply.setMode(Mode.PULL_FROM_END);// 上拉刷新
		lvReply.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(), System.currentTimeMillis(),
						flags);
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				// 获取数据
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				int flags = DateUtils.FORMAT_SHOW_TIME
						| DateUtils.FORMAT_SHOW_DATE
						| DateUtils.FORMAT_ABBREV_ALL;
				String label = DateUtils.formatDateTime(
						getApplicationContext(), System.currentTimeMillis(),
						flags);
				// 更新最后刷新时间
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				// 获取数据
				getData(refreshView);
			}
		});

		actualListView = lvReply.getRefreshableView();
		friendAdapter = new FriendAdapter(FriendActivity.this);
		friendAdapter.setCallBack(this);

//		btn_user_comment_emo = (Button) findViewById(R.id.btn_user_comment_emo);
//		edit_user_comment = (EmoticonsEditText) findViewById(R.id.edit_user_comment);
//		txtbtn_user_comment_send = (TextView) findViewById(R.id.btn_user_comment_send);
//		mInputView = (EmoteInputView) findViewById(R.id.chat_eiv_inputview);
//
//		btn_user_comment_emo.setOnClickListener(this);
	}

	protected void getData(PullToRefreshBase refreshView) {
		Map<String, String> paramMap = new HashMap<String, String>();
		currPage++;
		paramMap.put("currPage", currPage+"");
		RequestManager.getInstance(FriendActivity.this).post(
				NetConstans.FRIEND_CIRCLE, paramMap, new RequestListener() {
					@Override
					public void onSuccess(String response, String url,
							int actionId) {
						Track.i(response);
						TResponseBean<FriendCircle> data=gson.fromJson(response, new TypeToken<TResponseBean<FriendCircle>>() {}.getType());
						if(data.getStatus()==1){
							total=Integer.parseInt(data.getTotal());//总页数
							if(total>currPage){
								tmpPage = Integer.parseInt(data.getCurrPage());
								List<FriendCircle> list=data.getData();//朋友圈信息
								for (int i = 0; i < list.size(); i++) {
									dataList.add(list.get(i));
								}
								friendAdapter.setDataChanged(dataList);
							}else{
								currPage=tmpPage;
								ToastUtils.makeMesageShort(FriendActivity.this, "没有动态!");
							}
						}else{
							ToastUtils.makeMesageShort(FriendActivity.this,getString(R.string.server_error));
						}
						lvReply.onRefreshComplete();
						dismissPd();
					}

					@Override
					public void onRequest() {

					}
					@Override
					public void onError(String errorMsg, String url,
							int actionId) {
						ToastUtils.makeMesageShort(FriendActivity.this,getString(R.string.server_error));
						// 当数据加载完成，需要调用onRefreshComplete.
						lvReply.onRefreshComplete();
						dismissPd();
					}
				}, 1);
	}

	@Override
	public void ininlazeData() {
		showPd();
		Map<String, String> paramMap = new HashMap<String, String>();
		currPage = 1;
		paramMap.put("currPage", currPage+"");
		RequestManager.getInstance(FriendActivity.this).post(
				NetConstans.FRIEND_CIRCLE, paramMap, new RequestListener() {
					@Override
					public void onSuccess(String response, String url,
							int actionId) {
						Track.i(response);
						TResponseBean<FriendCircle> data=gson.fromJson(response, new TypeToken<TResponseBean<FriendCircle>>() {}.getType());
						if(data.getStatus()==1){
							total=Integer.parseInt(data.getTotal());//总页数
							if(data.getData().size()>0){
								tmpPage = Integer.parseInt(data.getCurrPage());
								dataList=data.getData();//朋友圈信息
								friendAdapter.setFriendCircles(dataList);
								actualListView.setAdapter(friendAdapter);
							}else{
								ToastUtils.makeMesageShort(FriendActivity.this, "没有动态!");
							}
						}else{
							ToastUtils.makeMesageShort(FriendActivity.this,getString(R.string.server_error));
						}
						dismissPd();
					}

					@Override
					public void onRequest() {

					}
					@Override
					public void onError(String errorMsg, String url,
							int actionId) {
						ToastUtils.makeMesageShort(FriendActivity.this,getString(R.string.server_error));
						dismissPd();
					}
				}, 1);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		Intent intent;
		int viewId = v.getId();
		switch (viewId) {
		
		case R.id.top_right_btn_image:
			intent = new Intent();
			intent.putExtra("hdxxId", "1");
			intent.setClass(this, PhotoActivity.class);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}


	@Override
	public void callBack() {
		ToastUtils.makeMesageShort(FriendActivity.this, "回调函数执行");
	}

	
	private void hideSoftInputView(Context context) {
		if (context == null)
			return;
		InputMethodManager manager = ((InputMethodManager) context
				.getSystemService(Activity.INPUT_METHOD_SERVICE));
		View view = ((Activity) context).getCurrentFocus();
		if (view == null)
			return;
		manager.hideSoftInputFromWindow(view.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 显示表情布局 showEmotionView
	 * 
	 * @throws
	 */
	private void showEmotionView1() {
		mInputView.setVisibility(View.VISIBLE);
	}
}
